package org.example.annotation;

import org.springframework.util.StringUtils;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
 * Package:                 com.bill99.fto.cmdc.pay.common.annotation
 * Date:                    2019/5/9 17:21
 * @Author:                kevin.wang
 * Company:                 kuaiqian
 * Version:                 1.0.0
 * Description:             类
 */
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = NotNull.Validator.class)
public @interface NotNull {

    String message() default "不能为空";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    public class Validator implements ConstraintValidator<NotNull, Object> {

        @Override
        public void initialize(NotNull constraint) {
        }

        @Override
        public boolean isValid(Object str, ConstraintValidatorContext context) {
            return StringUtils.isEmpty(str) ?  Boolean.FALSE : Boolean.TRUE;
        }
    }
}
